package com.plugin.planilha;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;

import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

public class AnaliseDefasadaVectorSheet implements ISheet {

	private WritableWorkbook workbook;
	private HashMap<String, HashSet<String>> mapVector;
	private HashMap<String, HashSet<String>> mapArraylist;
	private static WritableSheet defasadaVectorWS;

	public AnaliseDefasadaVectorSheet(WritableWorkbook w,
			HashMap<String, HashSet<String>> map_defasada,
			HashMap<String, HashSet<String>> map_usada) {
		workbook = w;
		this.mapVector = map_defasada;
		this.mapArraylist = map_usada;
		defasadaVectorWS = workbook.createSheet("Defasada Vector e ArrayList",
				1);
	}

	@Override
	public void addDados() throws RowsExceededException, WriteException {
		Label label;
		jxl.write.Number n;
		int indice = 1;
		double num_variaveis_defasadas = 0, num_variaveis_usadas = 0;

		for (String classe : mapArraylist.keySet()) {
			if (mapArraylist.get(classe).size() > 0) {
				Iterator<String> iterator = mapArraylist.get(classe).iterator();
				while (iterator.hasNext()) {
					String var = iterator.next();

					label = new Label(0, indice, var);
					defasadaVectorWS.addCell(label);
					indice++;
				}
			}
		}
		indice = 1;
		for (String clsse : mapVector.keySet()) {
			if (mapVector.get(clsse).size() > 0) {
				Iterator<String> iterator = mapVector.get(clsse).iterator();
				while (iterator.hasNext()) {
					String var = iterator.next();

					label = new Label(1, indice, var);
					defasadaVectorWS.addCell(label);
					indice++;
				}
			}
		}

		for (String classe : mapVector.keySet())
			num_variaveis_defasadas += mapVector.get(classe).size();
		for (String classe : mapArraylist.keySet())
			num_variaveis_usadas += mapArraylist.get(classe).size();

		double round_comparacao = 0;
		if (num_variaveis_defasadas != 0) {
			round_comparacao = (double) Math
					.round(((num_variaveis_defasadas * 100) / (num_variaveis_usadas + num_variaveis_defasadas)) * 1000) / 1000;
		}

		n = new jxl.write.Number(3, 1, (int) num_variaveis_defasadas);
		defasadaVectorWS.addCell(n);

		n = new jxl.write.Number(4, 1, (int) num_variaveis_usadas);
		defasadaVectorWS.addCell(n);

		n = new jxl.write.Number(5, 1, round_comparacao);
		defasadaVectorWS.addCell(n);

	}

	@Override
	public void criaCabecalho(WritableCellFormat cf)
			throws RowsExceededException, WriteException {

		Label label = new Label(0, 0, "Variaveis ArrayList", cf);
		defasadaVectorWS.addCell(label);

		label = new Label(1, 0, "Variaveis Vector", cf);
		defasadaVectorWS.addCell(label);

		label = new Label(3, 0, "Numero de instancias Vector", cf);
		defasadaVectorWS.addCell(label);

		label = new Label(4, 0, "Numero instancias de ArrayList", cf);
		defasadaVectorWS.addCell(label);

		label = new Label(5, 0, "Taxa de comparacao", cf);
		defasadaVectorWS.addCell(label);
	}

}
